/**   
 * @{#} TableNode.java Create on 2013-8-12 下午2:16:26   
 *   
 * Copyright (c) 2012 by Baidu.   
 */
package objtable.engine.xmlnode;

import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import org.supercsv.prefs.CsvPreference;

import com.google.common.base.Charsets;
import com.google.common.base.Joiner;
import com.google.common.io.Files;

import objtable.core.TableCenter;
import objtable.core.TableGenerator;
import objtable.engine.ExeNode;
import objtable.engine.XmlEngine;
import objtable.engine.xmlnode.gen.Export;
import objtable.engine.xmlnode.gen.Field;
import objtable.engine.xmlnode.gen.Load;
import objtable.engine.xmlnode.gen.Param;
import objtable.engine.xmlnode.gen.Table;

import objtable.port.CsvPort;
import objtable.port.DbPort;
import objtable.struct.DataType;
import objtable.struct.Row;

/**
 * @author <a href="mailto:zhangbo07@baidu.com">zhangbo07</a>
 * @version 1.0
 */
public class TableNode extends ExeNode {
	private Table table;

	public Table getTable() {
		return table;
	}

	public void setTable(Table table) {
		this.table = table;
	}

	@Override
	public void exeNode() {
		// 判断是否要构造table
		if (table.getField() != null) {
			TableGenerator tg = new TableGenerator(table.getId());
			for (Field f : table.getField()) {
				tg.addField(DataType.convertDT(f.getType()), f.getName(),
						getObject(f));
			}
			tg.regToCenter();
		}

		// 判断是否要装载数据进表
		if (table.getLoad() != null) {
			Load l = table.getLoad();
			// 数据库装载
			if (l.getDb() != null) {
				DbPort port = (DbPort) XmlEngine.getObjById(l.getDb());
				String sql = l.getSql();
				if (l.getParam() != null) {
					int i = 1;
					for (Param p : l.getParam()) {
						sql = sql.replace("#" + (i++), p.getValue());
					}
				}
				port.fromDB(table.getId(), sql);
			} else if (l.getCsvfile() != null && l.getCsvcols() != null) {
				if (l.getCsvsplit() == null) {
					CsvPort.fromCsv(table.getId(), l.getCsvfile(),
							l.getCsvcols(), CsvPreference.STANDARD_PREFERENCE);
				} else if (l.getCsvsplit().equals(",")) {
					CsvPort.fromCsv(table.getId(), l.getCsvfile(),
							l.getCsvcols(), CsvPreference.STANDARD_PREFERENCE);
				} else if (l.getCsvsplit().equals("t")) {
					CsvPort.fromCsv(table.getId(), l.getCsvfile(),
							l.getCsvcols(), CsvPreference.TAB_PREFERENCE);
				}
			}
		}
		// 判断是否要导出数据
		else if (table.getExport() != null) {
			Export e = table.getExport();
			if (e.getCsvfile() != null) {
				String fields = "";
				if (e.getCsvcols() == null) {
					String[] fs = TableCenter.getTable(table.getId())
							.getFields();
					Joiner j = Joiner.on(",");
					fields = j.join(fs);
				} else {
					fields = e.getCsvcols();
				}
				CsvPort.toCsv(table.getId(), e.getCsvfile(), fields);
			} else if (e.getSqlfile() != null) {
				List<Row> rows = TableCenter.getTableData(table.getId());
				String sql = e.getSql();
				List<Param> paras = e.getParam();
				List<String> result = new ArrayList<String>(rows.size());
				for (Row r : rows) {
					int i = 1;
					String t = sql;
					for (Param p : paras) {
						t = t.replaceAll("#" + (i++),
								TableCenter.getValueString(r, p));
					}
					result.add(t);
				}
				Joiner join = Joiner.on("\n");
				String all = join.join(result);

				try {
					Files.write(all, new File(e.getSqlfile()), Charsets.UTF_8);
				} catch (IOException e1) {
					throw new RuntimeException("生成文件失败:" + e.getSqlfile());
				}
			}
		}
	}

	private Object getObject(Field f) {
		if (f.getDefault() != null) {
			switch (f.getType()) {
			case DOUBLE:
				return Double.parseDouble(f.getDefault());
			case INT:
				return Integer.parseInt(f.getDefault());
			}
		}
		return null;
	}
}
